package com.zbbt.sportslottery.ui.activity;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore.Audio;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;
import com.umeng.message.PushAgent;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.App;
import com.zbbt.sportslottery.app.Constant;
import com.zbbt.sportslottery.app.UpdateVersion;
import com.zbbt.sportslottery.receiver.Utils;
import com.zbbt.sportslottery.util.AppUtil;
import com.zbbt.sportslottery.util.LogUtil;
import com.zbbt.sportslottery.util.PushUtils;
import com.zbbt.sportslottery.util.SharePreferUtil;
import com.zbbt.sportslottery.util.UmengUtil;

@SuppressLint("NewApi")
public class SplashActivity extends BaseActivity {
	private ImageView mView;
	private FrameLayout mContainer;
	private AnimatorSet animatorSet;
	private boolean isGoNext = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mView = createNewView();
		mContainer = new FrameLayout(context);
		mContainer.addView(mView);
		setContentView(mContainer);
		initialization();
		String deviceInfo = getDeviceInfo(context);
		LogUtil.printD("deviceInfo", deviceInfo);
		new Thread(new Runnable() {

			@Override
			public void run() {
				// 友盟
				UmengUtil.Init();
				// 检测版本更新
				UpdateVersion updateVersion = new UpdateVersion(context);
				updateVersion.requestUpdateData(UpdateVersion.PAGE_SPLASH);
			}
		}).start();
		mContainer.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (animatorSet != null) {
					animatorSet.cancel();
					goNext();
				}

			}
		});
	}

	private ImageView createNewView() {
		ImageView ret = new ImageView(this);
		ret.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		ret.setScaleType(ScaleType.FIT_XY);
		ret.setImageResource(R.drawable.state_pager);
		return ret;
	}

	private void goNext() {
		if (!isGoNext) {
			isGoNext = true;
			Intent intent;
			String versionName = AppUtil.getVersionName(context);
			boolean shownGuide = SharePreferUtil.getBoolean(versionName, false);
			if (!shownGuide) {
				intent = new Intent(context, GuideActivity.class);
				startActivity(intent);
				SharePreferUtil.putBoolean(versionName, true);
				new AddAliasTask(App.PARAM_DEVICEID, "default_type").execute();
			} else {
				intent = new Intent(context, MainActivity.class);
				startActivity(intent);
			}
			finish();
		}
	}

	/**
	 * 
	 * @MethodName:getProgarmList
	 * @Description: 初始动画
	 * @author：GaomingShuo
	 * @date：2015-3-13 下午2:49:48 void
	 */
	private void initialization() {
		animatorSet = new AnimatorSet();
		animatorSet.playTogether(ObjectAnimator.ofFloat(mView, "scaleX", 1f, 1.5f),
				ObjectAnimator.ofFloat(mView, "scaleY", 1f, 1.5f));
		animatorSet.setDuration(3000);
		animatorSet.addListener(new AnimatorListener() {

			@Override
			public void onAnimationStart(Animator animation) {

			}

			@Override
			public void onAnimationRepeat(Animator animation) {

			}

			@Override
			public void onAnimationEnd(Animator animation) {
				goNext();

			}

			@Override
			public void onAnimationCancel(Animator animation) {

			}
		});
		animatorSet.start();
	}

	public static String getDeviceInfo(Context context) {
		try {
			org.json.JSONObject json = new org.json.JSONObject();
			android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager) context
					.getSystemService(Context.TELEPHONY_SERVICE);

			String device_id = tm.getDeviceId();

			android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) context
					.getSystemService(Context.WIFI_SERVICE);

			String mac = wifi.getConnectionInfo().getMacAddress();
			json.put("mac", mac);

			if (TextUtils.isEmpty(device_id)) {
				device_id = mac;
			}

			if (TextUtils.isEmpty(device_id)) {
				device_id = android.provider.Settings.Secure.getString(context.getContentResolver(),
						android.provider.Settings.Secure.ANDROID_ID);
			}

			json.put("device_id", device_id);

			return json.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	class AddAliasTask extends AsyncTask<Void, Void, Boolean> {

		String alias;
		String aliasType;

		public AddAliasTask(String aliasString, String aliasTypeString) {
			// TODO Auto-generated constructor stub
			this.alias = aliasString;
			this.aliasType = aliasTypeString;
		}

		protected Boolean doInBackground(Void... params) {
			try {
				return PushAgent.getInstance(SplashActivity.this).addExclusiveAlias(alias, aliasType);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return false;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			if (result == true) {
				LogUtil.printD("umen", "successfully");
			} else {
				LogUtil.printD("umen", "faile");
			}
		}
	}
}
